package com.cms.handler;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cms.common.StringUtils;
import com.cms.constant.CmsConstant;
import com.cms.dao.CmsJdbcBaseDao;
import com.cms.report.RepDataHandlerService;

@Component("menuDataHandler")
public class MenuDataHandlerService implements RepDataHandlerService {

	@Autowired
	private CmsJdbcBaseDao cmsDao;
	
	@Override
	public Object handlerRepData(List<Map<String, Object>> srcData, Map<String, Object> query) throws Exception 
	{  
		if(srcData!=null&&!srcData.isEmpty())
		{
			Iterator<Map<String,Object>>iter= srcData.iterator();
			while(iter.hasNext())
			{
				Map<String,Object>data =iter.next(); 
				query.put("menuPid",data.get("id"));
				data.put("childrMenus",handlerMenuPorgram((List<Map<String,Object>>)this.cmsDao.getRepResult("loadChildMenus", query)));
			}
		}
		return srcData;
	}
	
	/**
	 * 
	 * @param menuUrl
	 */
	private List<Map<String,Object>> handlerMenuPorgram(List<Map<String,Object>>menus)
	{
		String menuUrl= null;
		String activityCode= null;
		for(int i=0;menus!=null&&i<menus.size();i++)
		{
	    Map<String,Object>map = menus.get(i);
		menuUrl=StringUtils.obj2String(map.get("menu_url"));
		activityCode =StringUtils.obj2String(map.get("menu_code"));
	    StringBuilder sb = new StringBuilder();
		if(!StringUtils.isBlank(menuUrl))
		{
			sb.append(menuUrl);
			if(menuUrl.contains("?"))
			{
				sb.append("&");
			}
			else
			{
				sb.append("?");
			}
			sb.append(CmsConstant.ACTIVITY_CODE).append("=").append(activityCode);
			map.put("menu_url", sb.toString());
		}
		}
		return menus;
	}

}
